﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using LinkDownloader.Core;
using System.Drawing;
using System.ComponentModel;

namespace LinkDownloader.Forms.JobLists
{

    internal class JobListCellProgress : DataGridViewCell
    {

        private static Brush progressBrush = Brushes.ForestGreen;
        private static StringFormat stringFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
        
        private JobList joblist = null;
        private DownloadJob download = null;

        public override Type FormattedValueType
        {
            get { return typeof(string); }
        }
                
        public JobListCellProgress(JobList joblist, DownloadJob download)
            : base()
        {
            this.joblist = joblist;
            this.download = download;
        }

        protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            //base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

            int width = (cellBounds.Width * this.download.Progress) / 100;
            Rectangle rectCell = new Rectangle(cellBounds.X, cellBounds.Y, cellBounds.Width, cellBounds.Height - 1);
            Rectangle rect = new Rectangle(cellBounds.X, cellBounds.Y, width, cellBounds.Height - 1);
            
            SolidBrush back = new SolidBrush((this.Selected) ? joblist.DefaultCellStyle.SelectionBackColor : joblist.DefaultCellStyle.BackColor);
            graphics.FillRectangle(back, rectCell);
            graphics.FillRectangle(progressBrush, rect);

            Font font = joblist.DefaultCellStyle.Font;
            string text = string.Format("{0} %", this.download.Progress);
            SizeF sz = graphics.MeasureString(text, font);
            PointF p = new PointF(cellBounds.X + (cellBounds.Width-sz.Width)/2,cellBounds.Y + (cellBounds.Height-sz.Height)/2);
            Brush fore = new SolidBrush((this.Selected) ? joblist.DefaultCellStyle.SelectionForeColor : joblist.DefaultCellStyle.ForeColor);
            //graphics.DrawString(text, font, fore, new RectangleF(p, sz), stringFormat);
            graphics.DrawString(text, font, fore, cellBounds, stringFormat);

            this.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);

        }

    }

}
